package com.nangle.managehost.fragmet;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.nangle.managehost.BaseFargment;
import com.nangle.managehost.R;
import com.nangle.managehost.adapter.ClientAdapter;
import com.nangle.managehost.adapter.DynamicAdapter;
import com.nangle.managehost.adapter.NurseAdapter;
import com.nangle.managehost.entity.BedMessage;
import com.nangle.managehost.entity.DayThree;
import com.nangle.managehost.entity.Doctor;
import com.nangle.managehost.entity.MessageDay;
import com.nangle.managehost.entity.OperationMsg;
import com.nangle.managehost.manager.DoctorManager;
import com.nangle.managehost.manager.NurseManager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 作者：Administrator on 2016/8/31 13:53
 * 描述：
 */
public class NurseFragment extends BaseFargment implements AdapterView.OnItemClickListener,View.OnTouchListener ,GestureDetector.OnGestureListener{
    private GridView mGridView;
    private List<Doctor> doctors = new ArrayList<>();
    private NurseAdapter mAdapter;

    private RelativeLayout rlyNurseDetail;
    private GestureDetector gestureDetector;
    private ListView lvDynamic,lvClient;
    private TextView tvName,tvAge,tvTitle;
    private static final int FLING_MIN_DISTANCE = 50;
    private static final int FLING_MIN_VELOCITY = 0;
    private boolean right_show = false;
    private DynamicAdapter dynamicAdapter;
    private ClientAdapter clientAdapter;
    private ImageView goBack;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.nurse_fragment, container, false);
        mGridView = (GridView) view.findViewById(R.id.nurseview_gridview);
        dynamicAdapter = new DynamicAdapter(mActivity);
        clientAdapter = new ClientAdapter(mActivity,false);
        mAdapter = new NurseAdapter(getActivity(), doctors);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(this);
        lvDynamic = (ListView)view.findViewById(R.id.list_dynamic);
        lvDynamic.setOnTouchListener(this);
        lvDynamic.setAdapter(dynamicAdapter);
        lvClient = (ListView)view.findViewById(R.id.list_client);
        lvClient.setOnTouchListener(this);
        lvClient.setAdapter(clientAdapter);
        tvName = (TextView)view.findViewById(R.id.tv_detail_name);
        tvAge = (TextView)view.findViewById(R.id.tv_detail_age);
        tvTitle = (TextView)view.findViewById(R.id.tv_detail_title);
        rlyNurseDetail = (RelativeLayout)view.findViewById(R.id.nurse_rlayout);
        gestureDetector = new GestureDetector(mActivity, this);
        rlyNurseDetail.setOnTouchListener(this);
        goBack = (ImageView)view.findViewById(R.id.back_bedcard) ;
        goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideRight();
            }
        });
//        setData();
        return view;

    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        setDetailContent(doctors.get(position));
        if(!right_show ){

            float curTranslationX = rlyNurseDetail.getTranslationX();
            ObjectAnimator animator = ObjectAnimator.ofFloat(rlyNurseDetail, "translationX", curTranslationX, -550f);
            ObjectAnimator animator_grid = ObjectAnimator.ofFloat(mGridView, "translationX", curTranslationX, -550f);
            animator.setDuration(500);
            animator_grid.setDuration(500);
            AnimatorSet animSet = new AnimatorSet();
            animSet.setDuration(500);
            animSet.setInterpolator(new LinearInterpolator());
            //两个动画同时执行
            animSet.playTogether(animator, animator_grid);
            // animSet.play();
            animSet.start();
            right_show = true;
        }

    }
    private void getData(){
        doctors = NurseManager.getInstance(mActivity).getDoctorList();

    }


    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e1.getX()-e2.getX() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // Fling left

        } else if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // Fling right
            hideRight();

        }
        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
    private void hideRight(){
        float curTranslationX = rlyNurseDetail.getTranslationX();
        ObjectAnimator animator = ObjectAnimator.ofFloat(rlyNurseDetail, "translationX", curTranslationX, 0f);
        ObjectAnimator animator_grid = ObjectAnimator.ofFloat(mGridView, "translationX", curTranslationX, 0f);
        animator.setDuration(500);
        animator_grid.setDuration(500);
        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(500);
        animSet.setInterpolator(new LinearInterpolator());
        //两个动画同时执行
        animSet.playTogether(animator, animator_grid);
        // animSet.play();
        animSet.start();
        right_show = false;

    }

    private void setDetailContent(Doctor detailContent) {
        tvName.setText(detailContent.getName());
        tvAge.setText(detailContent.getAge()+"岁");
        if(detailContent.getTitle().contains("<br>")){
            String[] strs = detailContent.getTitle().split(Pattern.quote("<br>"));
            tvTitle.setText(strs[0]+strs[1]);
        }else{
            tvTitle.setText(detailContent.getTitle());
        }
        dynamicAdapter.setData(detailContent.getDayThrees());
        dynamicAdapter.notifyDataSetChanged();

        clientAdapter.setData(detailContent.getBedMessages());
        clientAdapter.notifyDataSetChanged();
    }

    private void setData(){
        for(Doctor doctor : doctors){
            NurseManager.getInstance(mActivity).saveNurse(doctor);
        }
    }
}
